﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;

using Gift.WebApp;
using Gift.Model;

namespace WebApp.admin.goods
{
    public partial class goodsPromotion : BaseAdmin
    {

        tbGoodsPromotion oPromotion;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                if (Request.QueryString["ot"] == "add")
                {
                    this.ValidateLoginRule(Gift.Model.ENUM.ERule.GoodsPromotionAdd);
                }
                else
                {
                    this.ValidateLoginRule(Gift.Model.ENUM.ERule.GoodsPromotionEdit);
                }
                this.BindSelectGifts();
                this.BuidForm();
            }
        }

        void BindSelectGifts()
        {
            DataTable dt = BaseFactory.thePointsFor.GetAllGiftPointsFor();

                this.ddlSelectGift.DataTextField = "name";
                this.ddlSelectGift.DataValueField = "id";
                this.ddlSelectGift.DataSource = dt;
                this.ddlSelectGift.DataBind();
                this.ddlSelectGift.Items.Insert(0, new ListItem("请选择赠送礼品", ""));
              
        }

        protected void btnSubmit_onclick(object sender, EventArgs e)
        {
        }

        void BuidForm()
        {
            if (Request.QueryString["ot"] == "ed")
            {
                oPromotion = BaseFactory.theGoodsPromotion.GetModel(int.Parse(Request.QueryString["id"]));
                if (oPromotion != null)
                {
                    this.txtPromotionTitle.Value = oPromotion.promotionTitle;
                    this.selectPromotionType.Value = oPromotion.promotionType.ToString() ;
                    this.txtSortOrder.Value = oPromotion.sortOrder.ToString();
                    this.txtLimitAmount.Value = oPromotion.limitAmount.ToString();
                    this.txtFloorAmount.Value =  oPromotion.floorAmount.ToString();
                    this.txtDiscount.Value =  oPromotion.discount.ToString();
                    this.txtPreferentialAmount.Value =  oPromotion.preferentialAmount.ToString();
                    this.txtPresentedIntegral.Value = oPromotion.presentedIntegral.ToString();
                    if (oPromotion.isOverlap)
                    {
                        this.rdIsOverlap1.Checked = true;
                    }
                    else
                    {
                        this.rdIsOverlap0.Checked = false;
                    }
                    if (this.ddlSelectGift.Items.FindByValue(oPromotion.gift.ToString()) != null)
                    {
                        this.ddlSelectGift.Items.FindByValue(oPromotion.gift.ToString()).Selected = true;
                    }
                    this.txtDollarAmount.Value = oPromotion.dollarAmount == null ? "" : oPromotion.dollarAmount.ToString();

                    if (oPromotion.gift > 0)
                    {
                        this.hdEge.Value = oPromotion.gift.ToString();
                        tbGoods oGoods = BaseFactory.theGoods.GetModel(oPromotion.gift);
                        if (oGoods != null)
                        {
                            this.lblGoods.Value = oGoods.goodsName;
                        }
                    }
                    this.txtStartTime.Value = oPromotion.startTime.ToString("yyyy-MM-dd");
                    this.txtEndTime.Value = oPromotion.endTime.ToString("yyyy-MM-dd");
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["ot"] == "ed")
            {
                oPromotion = BaseFactory.theGoodsPromotion.GetModel(int.Parse(Request.QueryString["id"]));
            }
            else
            {
                oPromotion = new tbGoodsPromotion();
            }
            oPromotion.createTime = DateTime.Now;
            oPromotion.promotionTitle = this.txtPromotionTitle.Value.Trim();
            oPromotion.promotionType = int.Parse(this.selectPromotionType.Value);
            oPromotion.sortOrder = int.Parse(this.txtSortOrder.Value);
            oPromotion.floorAmount = decimal.Parse(this.txtFloorAmount.Value);
            oPromotion.startTime = DateTime.Parse(this.txtStartTime.Value);
            oPromotion.endTime = DateTime.Parse(this.txtEndTime.Value);
            if (oPromotion.promotionType == 2)
            {
                oPromotion.limitAmount = decimal.Parse(this.txtLimitAmount.Value);
                oPromotion.discount = decimal.Parse(this.txtDiscount.Value);
            }
            else  if (oPromotion.promotionType == 3)
            {
                oPromotion.limitAmount = decimal.Parse(this.txtLimitAmount.Value);
                oPromotion.preferentialAmount = decimal.Parse(this.txtPreferentialAmount.Value);
            }
            else if (oPromotion.promotionType == 4)
            {
                oPromotion.limitAmount = decimal.Parse(this.txtLimitAmount.Value);
                oPromotion.presentedIntegral = int.Parse(this.ddlSelectGift.SelectedItem.Value);
            }
            else if (oPromotion.promotionType == 5)
            {
                oPromotion.limitAmount = decimal.Parse(this.txtLimitAmount.Value);
                oPromotion.presentedIntegral = int.Parse(this.txtPresentedIntegral.Value);
                oPromotion.isOverlap = this.rdIsOverlap1.Checked;
            }
            else if (oPromotion.promotionType == 6)
            {
                oPromotion.limitAmount = decimal.Parse(this.txtLimitAmount.Value);
                oPromotion.presentedIntegral = int.Parse(this.txtPresentedIntegral.Value);
            }
            else if (oPromotion.promotionType == 7)
            {
                oPromotion.limitAmount = decimal.Parse(this.txtLimitAmount.Value);
                oPromotion.dollarAmount = this.txtDollarAmount.Value.Trim();
                oPromotion.gift = int.Parse(this.hdEge.Value);
            }

    
            if (Request.QueryString["ot"] == "ed")
            {
                if (BaseFactory.theGoodsPromotion.Update(oPromotion))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");window.location.href='goodsPromotionList.aspx';</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");window.location.href='goodsPromotionList.aspx';</script>");         
                }

            }
            else
            {
                oPromotion.CompanyId = int.Parse(base.Administrator.CompanyId);
                if (BaseFactory.theGoodsPromotion.Add(oPromotion) > 0)
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");window.location.href='goodsPromotionList.aspx';</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");window.location.href='goodsPromotionList.aspx';</script>");
                }

            }

        }
    }



}